////
//
/// \file   ./src/scoped_name_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/scoped_name_node.hpp>

#include <cassert>
#include <string>
#include <vector>

#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>

#include <inc/parser/internal_name_node.hpp>
#include <inc/parser/label_node.hpp>

namespace inc {
namespace parser {

using boost::scoped_ptr;
using boost::shared_ptr;
using std::string;

/// \brief  Default constructor
/// \author Chandler Carruth
/// \date   2008.05.16
///
/// Clear out all state and variables in preparation for parsing.

ScopedNameNode::ScopedNameNode()
    : object_scoped( false ),
      program_scoped( false ),
      base_name( NULL )
{}

/// \brief  Empty destructor
/// \author Chandler Carruth
/// \date   2008.05.16
///
/// Only exists here to facilitate header separation during compile.

ScopedNameNode::~ScopedNameNode() {}

/// \brief  Parse a scoped name
/// \author Chandler Carruth
/// \date   2007.07.26
///
/// This parses out a scoped name specification, including prefixed and
/// postfixed scope-selectors. The prefix can be a single or double "." where
/// a single "." selects tho object scope, and ".." selects the program scope.
/// The main component is some internal name representation. It may optionally
/// be repeatedly postfixed by a "." followed by a label, which builds up
/// a sub-scope selection chain. This is to reduce the nested groupings
/// necessary in the common case of seleting "foo.bar.baz()" or some similar
/// construt.

bool
ScopedNameNode::parse( const iterator &first,
                       const iterator &last,
                       iterator &last_parsed )
{
  assert( get_text().empty() && !base_name && !has_chain()
          && "This node was parsed more than once!" );

  //  Parse and flag any scope selection prefixes.
  iterator current = first;
  if( current < last && *current == '.' )
  {
    ++current;
    if( current < last && *current == '.' )
    {
      ++current;
      program_scoped = true;
    }
    else
    {
      object_scoped = true;
    }
  }

  //  Now parse the main name component
  boost::scoped_ptr<InternalNameNode> internal_name( new InternalNameNode );
  if( internal_name->parse( current, last, last_parsed ) )
  {
    current = last_parsed;
    base_name.swap( internal_name );

    //  Parse out any postfixed label selectors.
    while( current < last && *current == '.' )
    {
      ++current;

      boost::shared_ptr< LabelNode > label( new LabelNode );
      if( !label->parse( current, last, last_parsed ) )
      {
        --current;
        break;
      }
      current = last_parsed;
      name_chain.push_back( label );
    }
    
    //  We only succeed if there were no partial suffixes.
    if( current == last || *current != '.' )
    {
      last_parsed = current;
      set_text( string( first, last_parsed ) );
      return true;
    }
  }

  //  No valid parse found.
  last_parsed = first;
  program_scoped = false;
  object_scoped = false;
  base_name.reset();
  name_chain.clear();
  return false;
}

} //  end parser namespace
} //  end inc namespace
